﻿using AnimeLibrary.App_Code;

using System;
using System.Drawing;

namespace AnimeLibrary
{
    public partial class new_item : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                rgvNoOfEpisodes.MaximumValue = Int32.MaxValue.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (SaveNewDetails(txtTitle.Text, txtAuthor.Text, txtNoOfEpisodes.Text, txtPlotSummary.Text, uplImageLocation.FileName))
                Response.Redirect("list.aspx");
        }

        private bool SaveNewDetails(string title, string author, string noOfEpisodes, string plotSummary, string imageLocation)
        {
            bool saved = false;

            if ((imageLocation.Length > 0) && (IsImageFile(imageLocation)))
            {
                cvlImageLocation.IsValid = true;

                string newImageFileName = string.Format("{0}{1}", Guid.NewGuid().ToString(), imageLocation.Substring(imageLocation.LastIndexOf('.')));
                string saveImagePath = string.Format("images/{0}", newImageFileName);
                string thumbnailImagePath = string.Format("images/thumbnails/{0}", newImageFileName);

                if (WriteOp.AddAnime(Request.MapPath("App_Data\\AnimeLibrary.accdb"), title, author, int.Parse(noOfEpisodes), plotSummary, thumbnailImagePath, saveImagePath))
                {
                    uplImageLocation.SaveAs(Request.MapPath(saveImagePath));

                    Image image = Image.FromFile(Request.MapPath(saveImagePath));

                    // Start Keep Aspect Ratio
                    int thumbWidth = 100;
                    int thumbHeight = image.Height * thumbWidth / image.Width;
                    if (image.Height > image.Width)
                    {
                        thumbHeight = 100;
                        thumbWidth = image.Width * thumbHeight / image.Height;
                    }
                    // End Keep Aspect Ratio

                    Image thumb = image.GetThumbnailImage(thumbWidth, thumbHeight, () => false, IntPtr.Zero);
                    thumb.Save(Request.MapPath(thumbnailImagePath));

                    saved = true;
                }
            }
            else
            {
                cvlImageLocation.IsValid = false;
            }

            return saved;
        }

        private bool IsImageFile(string imageLocation)
        {
            bool isImage = false;

            switch (imageLocation.Substring(imageLocation.LastIndexOf('.')).ToLower())
            {
                case ".bmp":
                case ".gif":
                case ".jpeg":
                case ".jpg":
                case ".tif":
                case ".png":
                    isImage = true;
                    break;
            }

            return isImage;
        }
    }
}